<?php

declare(strict_types=1);
/**
 * This file is part of habo_new_media.
 *
 * @link     https://www.berlen.cn/
 * @contact  tech@berlen.cn
 * @license  http://gitlab.rehou.cn/habo/habo_new_media/-/blob/master/README.md
 */
namespace app\service\analysis\data\material;

use app\enum\AnalysisEnum;
use app\service\analysis\data\material\pic\PicDayStatService;
use app\service\analysis\data\material\video\VideoDayStatService;
use app\service\BaseService;

final class MaterialDayStatService extends BaseService
{
    private int $mediaId;

    private string $signature;

    private int $statDate;

    private int $taskType;

    public function __construct()
    {
    }

    /**
     * 任务执行.
     * @param array $arr 参数
     * @return bool
     */
    public function runTask(array $arr): bool
    {
        $this->mediaId = intval($arr[0] ?? 0);
        $this->taskType = intval($arr[1] ?? 0);
        $this->signature = strval($arr[2] ?? '');
        $this->statDate = intval($arr[3] ?? 0);

        if (! $this->checkProperty()) {
            return false;
        }

        if (empty($this->signature)) {
            return true;
        }

        if ($this->taskType == AnalysisEnum::TASK_TYPE_VIDEO) {
            return (new VideoDayStatService($this->mediaId, $this->signature, $this->statDate))->setStat();
        }

        if ($this->taskType == AnalysisEnum::TASK_TYPE_PICTURE) {
            return (new PicDayStatService($this->mediaId, $this->signature, $this->statDate))->setStat();
        }
        return self::setError('未知任务类型');
    }

    /**
     * 属性检查.
     */
    private function checkProperty(): bool
    {
        if (empty($this->mediaId)) {
            return self::setError('未知媒体');
        }
        if (empty($this->taskType)) {
            return self::setError('未知任务类型');
        }
        if (empty($this->statDate)) {
            return self::setError('未知汇总日期');
        }
        return true;
    }
}
